package com.ruoyi.service.impl;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.domain.T01;
import com.ruoyi.domain.T03;
import com.ruoyi.domain.T06;
import com.ruoyi.domain.T12DTO;
import com.ruoyi.domain.T12VO;
import com.ruoyi.domain.BulkResponsesDTO;
import com.ruoyi.mapper.T01Mapper;
import com.ruoyi.mapper.T03Mapper;
import com.ruoyi.service.IT06Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.T12Mapper;
import com.ruoyi.domain.T12;
import com.ruoyi.service.IT12Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 信息反馈Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-28
 */
@Service
public class T12ServiceImpl implements IT12Service
{
    @Autowired
    private T12Mapper t12Mapper;
    @Autowired
    private T03Mapper t03Mapper;
    @Autowired
    private IT06Service t06Service;
    @Autowired
    private T01Mapper t01Mapper;
    /**
     * 查询信息反馈
     *
     * @param f1201 信息反馈主键
     * @return 信息反馈
     */
    @Override
    public T12VO selectT12ByF1201(Long f1201)
    {
        T12 t12 = t12Mapper.selectT12ByF1201(f1201);
        T03 t03 = t03Mapper.selectT03ByF0301(t12.getF0301());
        T06 t06 = t06Service.selectT06ByF0601(t12.getF0601());
        T12VO t12VO=new T12VO();
        BeanUtils.copyProperties(t12,t12VO);
        t12VO.setF0302(t03.getF0302());//公司名
        t12VO.setF0604(t06.getF0604());//电话
        t12VO.setF1202(t06.getF0602());//姓名
        return t12VO;
    }

    /**
     * 查询信息反馈列表
     * @param t12
     * @return
     */
    @Override
    @Transactional
    public List<T12VO> selectT12V0List(T12DTO t12) {
        List<T12> t12List = t12Mapper.selectT12List(t12);
        List<Long> f0301s = new ArrayList<>();
        List<Long> f0601s = new ArrayList<>();
        List<Long> f0101s = new ArrayList<>();
        List<T12VO> t12VOList = new ArrayList<>();
        List<T03> t03List = new ArrayList<>();
        List<T06> t06List = new ArrayList<>(); // 提前初始化
        List<T01> t01List = new ArrayList<>();
        for (T12 item : t12List) {
            f0301s.add(item.getF0301());
            f0601s.add(item.getF0601());
            f0101s.add(item.getF0101());
        }

        if (!f0301s.isEmpty()) {
            // 调用t03Mapper获得F0302
            t03List = t03Mapper.selectT03ListByF0301s(f0301s);
        }
        if (!f0601s.isEmpty()) {
            // 调用t06Mapper获得 String f0604 和 String f0602
            t06List = t06Service.selectT06ListByF0601s(f0601s);
        }
        if (!f0101s.isEmpty()) {
            // 调用t01Mapper获得F0103
            t01List = t01Mapper.selectT01ListByF0101s(f0101s);
        }

        for (int i = 0; i < f0301s.size(); i++) {
            T12VO t12VO = new T12VO();
            // 确保访问t06List、t03List和t01List时它们已经被初始化
            BeanUtils.copyProperties(t12List.get(i), t12VO);
            if (i < t06List.size()) {
                t12VO.setF0604(t06List.get(i).getF0604()); // 电话
                t12VO.setF1202(t06List.get(i).getF0602()); // 姓名
            }
            if (i < t03List.size()) {
                t12VO.setF0302(t03List.get(i).getF0302()); // 公司名
            }
            if (i < t01List.size()) {
                t12VO.setF0103(t01List.get(i).getF0103()); // 员工名
            }
            t12VOList.add(t12VO);
        }

        return t12VOList;
    }

    /**
     * 批量回复信息
     * @param bulkResponsesDTO
     *
     */
    @Override
    public void bulkResponses(BulkResponsesDTO bulkResponsesDTO) {
        //todo 回复人的id需要登录是存储后在此处存入
        bulkResponsesDTO.setF0101(String.valueOf(1));
        bulkResponsesDTO.setF1205(1);//改为已回复
        bulkResponsesDTO.setF1206(LocalDate.now());
        t12Mapper.bulkResponses(bulkResponsesDTO);
    }

    @Override
    public void updateStatusProcessed(BulkResponsesDTO bulkResponsesDTO) {
        List<Long> f1201s = bulkResponsesDTO.getF1201s();
        List<T12> t12List =t12Mapper.getT12F1205ByF1201s(f1201s,0);
        if(!t12List.isEmpty()){
            throw new RuntimeException("还有未回复的信息");
        }
        bulkResponsesDTO.setF1205(2);
        t12Mapper.bulkResponses(bulkResponsesDTO);
    }

    /**
     * 新增信息反馈
     *
     * @param t12 信息反馈
     * @return 结果
     */
    @Override
    public int insertT12(T12 t12)
    {
        return t12Mapper.insertT12(t12);
    }

    /**
     * 修改信息反馈
     *
     * @param t12 信息反馈
     * @return 结果
     */
    @Override
    public int updateT12(T12 t12)
    {
        return t12Mapper.updateT12(t12);
    }

    /**
     * 批量删除信息反馈
     *
     * @param f1201s 需要删除的信息反馈主键
     * @return 结果
     */
    @Override
    public int deleteT12ByF1201s(Long[] f1201s)
    {
        return t12Mapper.deleteT12ByF1201s(f1201s);
    }

    /**
     * 删除信息反馈信息
     *
     * @param f1201 信息反馈主键
     * @return 结果
     */
    @Override
    public int deleteT12ByF1201(Long f1201)
    {
        return t12Mapper.deleteT12ByF1201(f1201);
    }

}
